package com.deco.service.sys;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.deco.domain.vo.constants.CmsConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.deco.domain.vo.sys.ChinastatesVo;
import com.deco.logic.sys.ChinastatesLogic;

@Service
public class ChinastatesService {
	   /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/
    
    @Autowired
    private ChinastatesLogic chinastatesLogic;


    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    @Cacheable(value = "chinaStates")
    public  Map<String, ChinastatesVo> getAll(){
    	Map<String, ChinastatesVo> pMap = null;
    	 // 省
        pMap = new LinkedHashMap<String, ChinastatesVo>();
        ChinastatesVo paramChinastatesVo = new ChinastatesVo();
        paramChinastatesVo.setPcode("0001");
        List<ChinastatesVo> pList = chinastatesLogic.selectList(paramChinastatesVo);
        for (ChinastatesVo p : pList) {
            p.setChildMap(new LinkedHashMap<String, ChinastatesVo>());
            pMap.put(p.getCode(), p);
        }
    	
        // 市
        Map<String, ChinastatesVo> cMap = new LinkedHashMap<String, ChinastatesVo>();
        paramChinastatesVo = new ChinastatesVo();
        List<ChinastatesVo> cList = chinastatesLogic.selectList(paramChinastatesVo);
        if(cList != null && cList.size() > 0){
        	for(ChinastatesVo chinastatesVo : cList){
        		if(pMap.containsKey(chinastatesVo.getPcode())){
        			chinastatesVo.setChildMap(new LinkedHashMap<String, ChinastatesVo>());
        			cMap.put(chinastatesVo.getCode(), chinastatesVo);
        		}
        	}
        	
        	//县
        	for(ChinastatesVo chinastatesVo : cList){
        		if(cMap.containsKey(chinastatesVo.getPcode())){
        			cMap.get(chinastatesVo.getPcode()).getChildMap().put(chinastatesVo.getCode(), chinastatesVo);
        		}
        	}
        }
        
        
        //把市放入省
        // 市放入省
        for (Entry<String, ChinastatesVo> c : cMap.entrySet()) {
            if (pMap.containsKey(c.getValue().getPcode())) {
                pMap.get(c.getValue().getPcode()).getChildMap().put(c.getKey(), c.getValue());
            }
        }
    	
    	return pMap;
	}

    public ChinastatesVo findByCode(String code) {
        ChinastatesVo cs = null;
        Map<String, ChinastatesVo> allMap = getAll();
        if (code.equals(CmsConstant.CHINA_ROOT_CODE)) {
            cs = new ChinastatesVo();
            cs.setChildMap(allMap);
            cs.setCode(CmsConstant.CHINA_ROOT_CODE);
            cs.setName("中国");
            cs.setPcode("0000");
            return cs;
        } else if (code.length() == 2) {
            return allMap.get(code);
        } else if (code.length() == 4) {
            return allMap.get(code.substring(0, 2)).getChildMap().get(code);
        } else if (code.length() == 6) {
            return allMap.get(code.substring(0, 2)).getChildMap().get(code.subSequence(0, 4)).getChildMap().get(code);
        }
        return null;
    }

    public Map<String, ChinastatesVo> findChildByPcode(String pcode) {
        if (pcode.equals(CmsConstant.CHINA_ROOT_CODE)) {
            return getAll();
        } else {
            ChinastatesVo cs = findByCode(pcode);
            if (cs != null) {
                return cs.getChildMap();
            }
        }
        return null;
    }

    public String findPathByCode(String code, String splitSign) {
        StringBuffer sb = new StringBuffer();
        ChinastatesVo cs = null;
        Map<String, ChinastatesVo> allMap = getAll();
        if (code.length() == 2) {
            sb.append(allMap.get(code).getName());
        } else if (code.length() == 4) {
            cs = allMap.get(code.substring(0, 2));
            sb.append(cs.getName()).append(splitSign);
            cs = cs.getChildMap().get(code);
            sb.append(cs.getName());
        } else if (code.length() == 6) {
            cs = allMap.get(code.substring(0, 2));
            sb.append(cs.getName()).append(splitSign);
            cs = cs.getChildMap().get(code.substring(0, 4));
            sb.append(cs.getName()).append(splitSign);
            cs = cs.getChildMap().get(code);
            sb.append(cs.getName());
        }
        return sb.toString();
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/

}
